package com.owc.controller.admin;

import com.owc.common.custom.annotation.Advice;
import com.owc.domain.BaseResult;
import com.owc.domain.dto.BannerDTO;
import com.owc.domain.vo.admin.QueryVo;
import com.owc.domain.vo.admin.BannerVo;
import com.owc.service.app.BannerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * @Author xiaom
 * @Date 2020/3/2 15:48
 * @Version 1.0.0
 * @Description <>
 **/
@RestController
@RequestMapping(value = "/admin/banner")
@Api(tags = "(manage-V2)banner管理")
public class BannerAdminController {

    private final BannerService bannerService;

    public BannerAdminController(BannerService bannerService) {
        this.bannerService = bannerService;
    }

    @PostMapping(value = "/add")
    @ApiOperation("新建banner")
    public BaseResult<Boolean> add(@RequestBody @Valid BannerVo bannerVo) throws Exception {
        return BaseResult.success(bannerService.save(bannerVo.converterTo()));
    }

    @PostMapping(value = "/list")
    @ApiOperation("获取banner列表")
    public BaseResult<BannerDTO> list(@RequestBody QueryVo queryVo){
        return BaseResult.makePageBean(bannerService.pageList(queryVo));
    }

    @PostMapping(value = "/update")
    @ApiOperation("更新banner")
    public BaseResult<Boolean> update(@RequestBody BannerVo bannerVo) throws Exception {
        Assert.notNull(bannerVo.getId(),"ID不能为空");
        return BaseResult.success(bannerService.update(bannerVo));
    }

    @GetMapping(value = "/detail")
    @ApiOperation("获取banner详情")
    public BaseResult<BannerDTO> detail(@RequestParam @Valid @NotNull(message = "id不能为空") Long id){
        return BaseResult.success(bannerService.getById(id));
    }
}
